#include "vpagelayout.h"
#include <QHBoxLayout>

vPageLayout::vPageLayout(QWidget *parent) : QWidget(parent)
{
    //整体布局为水平布局
    layoutall = new QVBoxLayout(this);

    //新建网格框体
    vMtabWidget = new QTabWidget();

    // 创建第一个选项卡页面(2D显示选卡)
    QWidget *tab1 = new QWidget;
    QVBoxLayout *layout1 = new QVBoxLayout;
    layout1->addWidget(vChartData);
    tab1->setLayout(layout1);

    // 创建第二个选项卡页面(3D显示选卡)
    QWidget *tab2 = new QWidget;
    QVBoxLayout *layout2 = new QVBoxLayout;
    layout2->addWidget(v3DWidget);
    tab2->setLayout(layout2);

    // 添加选项卡到QTabWidget
    vMtabWidget->addTab(tab1, "2D");
    vMtabWidget->addTab(tab2, "3D");

    //创建按钮
    vFileSeletPushButton = new QPushButton("...");
    //创建水平布局
    QHBoxLayout *vMainLayout = new QHBoxLayout();
    //将选框等按钮放入水平布局之中
    QLabel *label = new QLabel("数据地址：");
    vMainLayout->addWidget(label);
    vFileAddrLineEdit = new QLineEdit();
    vMainLayout->addWidget(vFileAddrLineEdit);
    vMainLayout->addWidget(vFileSeletPushButton); //rotateLeftButton
    vMainLayout->addWidget(rotateLeftButton);
    vMainLayout->addWidget(rotateRightButton);

    layoutall->addWidget(vMtabWidget);
    layoutall->addLayout(vMainLayout);

    connect(vFileSeletPushButton,&QPushButton::clicked,this,&vPageLayout::openFileDialog);
    connect(this,&vPageLayout::vCulDarwNewLine,this->vChartData,&vChartWidget::vCulDarwNewLine);
    connect(this,&vPageLayout::vCulDarw3DLine,this->v3DWidget,&c3DWidget::vDrawNewWell);

    // 连接按钮的点击信号到槽函数
    connect(rotateLeftButton, &QPushButton::clicked, this->v3DWidget, &c3DWidget::rotateLeft);
    connect(rotateRightButton, &QPushButton::clicked, this->v3DWidget, &c3DWidget::rotateRight);

}

vPageLayout::~vPageLayout(void)
{

}



/* 选择文件并打开操作 */
void vPageLayout::openFileDialog(void)
{
    // 打开文件选择对话框
    QString filePath = QFileDialog::getOpenFileName(this, "选择文件", "", "Text Files (*.txt);;CSV Files (*.csv)");
    // 判断是否有文件
    if(filePath.isEmpty())
    {
        QMessageBox msgBox(this);
        msgBox.setWindowTitle("提示");
        msgBox.setText("未选择文件");

        // 将弹出窗口移动到主窗口的中心位置
        QPoint centerPos = this->geometry().center() - msgBox.rect().center();
        msgBox.move(this->mapToGlobal(centerPos));
        // 显示消息框
        msgBox.exec();

    }
    else
    {
        this->vFileAddrLineEdit->setText(filePath);
        this->readFiletoTab(filePath);
        //判断数据是否存在，存在就写入

       /* 获得数据后开始绘制2D线 */
        emit vCulDarwNewLine(vInputRelat);
       /* 绘制3D图形 */
        emit vCulDarw3DLine(vInputRelat);
    }
}


// 工具函数：读取文件内容
void vPageLayout::readFiletoTab(const QString filePath)
{
    RelatWellData lineRelat;

    QFile file(filePath);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        lineRelat.eNumb = 0;
        //循环读取数据
        for(int n=0;;n++)
        {
            //判断是否已经读取到最后
            if(!in.atEnd())
            {
                //逐行读取数据
                QString line = in.readLine();
                //逐个单元格放入表格
                QStringList datas = line.split(" ");
                //写入放到入到数据显示点内
                lineRelat.eNumb++;
                lineRelat.eDepth = datas.at(0).toFloat();
                lineRelat.eAzimuth = datas.at(1).toFloat();
                lineRelat.eDistan = datas.at(2).toFloat();
                qDebug() << lineRelat.eNumb << lineRelat.eDepth << lineRelat.eDistan << lineRelat.eAzimuth;
                vInputRelat.append(lineRelat);
            }
            else
            {
                break;
            }
        }
        file.close();
    }
    else
    {
        // 文件打开失败
        qDebug() << "Failed to open the file.";
    }
    //    QString content = in.readAll();
    return;
}

















